﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YTCMS.Core;
using YTCMS.Domain.Entities;
using YTCMS.Services.Services;

namespace Admin.Controllers
{
    public class ContentController : BaseController
    {
        private readonly IcontentService _content;
        private readonly IcmscolumnService _cmscolumn;
        public ContentController(IcontentService content, IcmscolumnService cmscolumn)
        {
            this._cmscolumn = cmscolumn;
            this._content = content;
        }
        //
        // GET: /Content/
        public ActionResult Index(int? pageIndex = 1, int? pageSize = 10)
        {
            var data = _cmscolumn.GetAsyncMany(s => s.State == (int)CommonState.正常);
            ViewData["module"] = data;
            int pcount = 0;
            var model = _content.GetMany(s => true, Order.desc, "Sort");
            model = _content.PageList(model, pageIndex.Value - 1, pageSize.Value, out pcount);
            ViewBag.PageSize = pageSize.Value;
            ViewBag.TotalCount = pcount;
            return View(model);
        }

        public ActionResult Search(int? mid, int? pageIndex = 1, int? pageSize = 10)
        {
            int pcount;
            var model = _content.GetMany(s => true, Order.desc, "Sort");
            model = QueryHelper.WhereIf<content>(model, s => s.Cid == mid, mid != null);
            model = _content.PageList(model, pageIndex.Value - 1, pageSize.Value, out pcount);
            ViewBag.PageSize = pageSize.Value;
            ViewBag.TotalCount = pcount;
            return PartialView("List", model);
        }


        public ActionResult add()
        {
            return View();
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult add(content model)
        {
            if (ModelState.IsValid)
            {
                model.Images = Request["hidimg"];

                model.CreateTime = System.DateTime.Now;
                _content.Add(model);
            }
            return new JsonResult() { Data = new { d = true } };
        }


        public ActionResult Edit(int id)
        {
            var model = _content.GetAsync(s => s.Id == id);
            return View(model);
        }
        [HttpPost]
        public ActionResult Edit(content model)
        {
            if (ModelState.IsValid)
            {
                _content.Update(model);
            }
            return new JsonResult() { Data = new { d = true } };
        }

        public ActionResult Delete(int id)
        {
            var model = _content.Get(s => s.Id == id);
            _content.Delete(model);
            return new JsonResult() { Data = true, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }
	}
}